package com.allwees.bs.module.lms.track;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/9/19 18:11
 */
class RetrofitManager {

    private static final String BASE_51_TRACKING_URL = "https://api.51tracking.com/v2/trackings/";
    private static final String TRACKING_51_APIKEY = "d8d9a7ea-3f29-4c0c-95f9-f3f72f13c9fd";

    private static final String BASE_AFTERSHIP_TRACKING_URL = "https://api.aftership.com/v4/";
    private static final String TRACKING_AFTERSHIP_APIKEY = "01e88c08-44b1-457a-9304-6ab9f8636276";

    private final Retrofit tracking51Retrofit;
    private Retrofit afterShipRetrofit;

    private Retrofit iMileRetrofit;

    private static  Map<String,String> afterShipSlugMap = new HashMap<>();


    private RetrofitManager(String imileBaseUrl) {
        OkHttpClient ok51TrackingHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new Header51TrackingInterceptor())
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10,TimeUnit.SECONDS)
                .build();
        OkHttpClient okAfterShipHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HeaderAfterShipInterceptor())
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10,TimeUnit.SECONDS)
                .build();

        OkHttpClient okMileClient = new OkHttpClient.Builder()
                .addInterceptor(new HeaderMileInterceptor())
                .connectTimeout(10,TimeUnit.SECONDS)
                .readTimeout(10,TimeUnit.SECONDS)
                .build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        tracking51Retrofit = new Retrofit.Builder()
                .baseUrl(BASE_51_TRACKING_URL)
                .client(ok51TrackingHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        afterShipRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_AFTERSHIP_TRACKING_URL)
                .client(okAfterShipHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        iMileRetrofit = new Retrofit.Builder()
                .baseUrl(imileBaseUrl)
                .client(okMileClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        afterShipSlugMap.put("dhl","dhl-global-mail");
    }

    private static volatile RetrofitManager retrofitManager = null;

    static RetrofitManager getInstance(String imileBaseUrl) {
        if (retrofitManager == null) {
            synchronized (TrackingHelper.class) {
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager(imileBaseUrl);
                }
            }
        }
        return retrofitManager;
    }

    Retrofit getTracking51Retrofit() {
        return tracking51Retrofit;
    }

    Retrofit getAfterShipRetrofit() {
        return afterShipRetrofit;
    }

    Retrofit getIMileRetrofit() {
        return  iMileRetrofit;
    }

    static  String getAfterShipSlug(String slug){
        return afterShipSlugMap.get(slug.toLowerCase());
    }

    static class Header51TrackingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder()
                    .addHeader("Tracking-Api-Key", TRACKING_51_APIKEY)
                    .addHeader("Content-Type", "application/json")
                    .build();
            return chain.proceed(request);
        }
    }

    static class HeaderAfterShipInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder()
                    .addHeader("aftership-api-key", TRACKING_AFTERSHIP_APIKEY)
                    .addHeader("Content-Type", "application/json")
                    .build();
            return chain.proceed(request);
        }
    }
    static class HeaderMileInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder()
                    .addHeader("Content-Type", "application/json")
                    .build();
            return chain.proceed(request);
        }
    }
}
